package com.woniuxy.lrz.carlproject.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.controller.form.userform.AddOrderForm;
import com.woniuxy.lrz.carlproject.controller.form.orderform.UpdateOrderForm;
import com.woniuxy.lrz.carlproject.model.UserOrder;
import com.woniuxy.lrz.carlproject.service.UserOrderService;
import com.woniuxy.lrz.carlproject.service.dto.orderDto.OrderDetailDTO;
import com.woniuxy.lrz.carlproject.service.dto.orderDto.UserOrderListDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import javax.validation.Valid;

/**
 * <p>
 * 用户订单表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/userOrder")
@Slf4j
@Api("用户订单接口")
public class UserOrderController {

    @Resource
    private UserOrderService userOrderService;

    @GetMapping("/getOrderByShopId")
    @Log("门店查询订单")
    @ApiOperation("门店查询订单")
    public Result getOrderByShopId(@RequestParam(defaultValue = "1")int current, @RequestParam(defaultValue = "10")  int size){
        int shopId = Integer.parseInt((String) StpUtil.getLoginId());
        log.info("门店id:{}",shopId);
        if (shopId<1 ){
            throw new AppException("参数错误");
        }
        Page<UserOrder> order = userOrderService.getOrderByShopId(current, size, shopId);
        return Result.success(order);
    }

    @PostMapping("/updateOrderStatus")
    @Log("门店修改订单状态")
    @ApiOperation("门店修改订单状态")
    @ApiImplicitParam("修改订单状态表单")
    public  Result uodateOrderStatus(@Valid @RequestBody UpdateOrderForm updateOrderForm){
        log.info("表单",updateOrderForm);
        userOrderService.updateOrderStatus(updateOrderForm.getUserOrderNo(), updateOrderForm.getStatus());
        return Result.success();
    }


    //用户订单新增 返回订单编号
    @PostMapping("/addOrder")
    @Log("用户订单新增")
    @ApiOperation("用户订单新增")
    public  Result addOrder(@RequestBody AddOrderForm addOrderForm){
        String order = userOrderService.addOrder(addOrderForm);
        return Result.success(order);
    }
    //用户取消订单  只有未支付才能取消
    @PutMapping("/cancelOrder")
    @Log("用户取消订单")
    @ApiOperation("用户取消订单")
    @ApiImplicitParam("订单编号")
    public  Result cancelOrder(String userOrderNo){
        log.info("订单id:{}",userOrderNo);
        userOrderService.cancelOrder(userOrderNo);
        return Result.success("取消订单成功");
    }
    //用户查看订单列表
    @GetMapping("/getOrders")
    @Log("用户查看订单列表")
    @ApiOperation("用户查看订单列表")
    public  Result getOrders(){
        List<UserOrderListDTO> orders = userOrderService.getOrders();
        return Result.success(orders);
    }
    //用户查看订单详情
    @GetMapping("/getOrderDetail")
    @Log("用户查看订单详情")
    @ApiOperation("用户查看订单详情")
    public  Result getOrderDetail(String userOrderNo){
        OrderDetailDTO detailDTO = userOrderService.getOrderDetail(userOrderNo);
        return Result.success(detailDTO);
    }
    //用户签收订单  只有服务完成状态 才可以改为签收状态
    @PutMapping("/confirmOrder")
    @Log("用户签收订单")
    @ApiOperation("用户签收订单")
    @ApiImplicitParam("订单编号")
    public  Result confirmOrder(@RequestParam("userOrderNo") String userOrderNo){
        userOrderService.confirmOrder(userOrderNo);
        return Result.success("签收成功");
    }

    //支付订单
    @GetMapping("/doPay")
    @Log("支付订单")
    @ApiOperation("支付订单")
    public Result doPay(String traceNo,String totalAmount,String subject){
        String payUrl = "http://127.0.0.1:8081/pay?subject=" + subject +"&traceNo=" +traceNo
                + "&totalAmount=" + totalAmount;
        return Result.success(payUrl);
    }

}
